#include <stdio.h>
#include <string.h>
#include <stdlib.h>

#define MAX 200005

int n;
int w[MAX];
int parent[MAX];
int head[MAX];
int to[MAX];
int next[MAX];
int edge_count;
int out_degree[MAX];
long long dp[MAX][2];

void add_edge(int u, int v)
{
    to[edge_count] = v;
    next[edge_count] = head[u];
    head[u] = edge_count++;
}

int main() 
{
    scanf_s("%d", &n);
    for (int i = 1; i <= n; i++) 
    {
        scanf_s("%d", &w[i]);
    }
    memset(head, -1, sizeof(head));
    edge_count = 0;
    memset(parent, 0, sizeof(parent));
    memset(out_degree, 0, sizeof(out_degree));

    for (int i = 0; i < n - 1; i++) 
    {
        int k, l;
        scanf_s("%d %d", &k, &l);
        parent[l] = k;
        add_edge(k, l);
        out_degree[k]++;
    }

    for (int i = 1; i <= n; i++)
    {
        dp[i][0] = 0;
        dp[i][1] = w[i];
    }

    int queue[MAX];
    int front = 0, rear = 0;
    for (int i = 1; i <= n; i++)
    {
        if (out_degree[i] == 0) 
        {
            queue[rear++] = i;
        }
    }

    while (front < rear)
    {
        int u = queue[front++];
        int p = parent[u];
        if (p != 0)
        {
            if (dp[u][0] > dp[u][1]) 
            {
                dp[p][0] += dp[u][0];
            }
            else 

            {
                dp[p][0] += dp[u][1];
            }
            dp[p][1] += dp[u][0];
            out_degree[p]--;
            if (out_degree[p] == 0) 

            {
                queue[rear++] = p;
            }
        }
    }

    int root = 0;
    for (int i = 1; i <= n; i++)
    {
        if (parent[i] == 0)
        {
            root = i;
            break;
        }
    }

    long long ans = dp[root][0] > dp[root][1] ? dp[root][0] : dp[root][1];
    printf("%lld\n", ans);

    return 0;
}